package com.shujia.json;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;
import java.util.List;

public class Demo2Xml {
    public static void main(String[] args) throws DocumentException {

        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read(new File("java/data/books.xml"));


        //获取根节点
        Element rootElement = document.getRootElement();

        //获取子节点
        Iterator iterator = rootElement.elementIterator();

        while (iterator.hasNext()) {
            //一个子节点
            Element next = (Element) iterator.next();

            //获取所有属性
            Iterator iterator1 = next.attributeIterator();

            while (iterator1.hasNext()) {
                Attribute next1 = (Attribute) iterator1.next();
                System.out.println(next1.getName() + "===" + next1.getValue());
            }


            //获取子节点
            Iterator iterator2 = next.elementIterator();
            while (iterator2.hasNext()) {
                Element next2 = (Element) iterator2.next();

                //获取文本内容
                String text = next2.getText();
                System.out.println(text);
            }

        }


    }
}
